package com.kaili.basic.service.impl;

import com.baomidou.mybatisplus.plugins.Page;
import com.kaili.basic.mapper.BaseMeasurementMapper;
import com.kaili.basic.pojo.dto.BaseMeasurementAddDto;
import com.kaili.basic.pojo.dto.BaseMeasurementQueryDto;
import com.kaili.basic.pojo.entity.BaseMeasurement;
import com.kaili.basic.pojo.entity.BaseMeasurementExport;
import com.kaili.basic.service.BaseMeasurementService;
import com.kaili.common.util.LoginDetailUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class BaseMeasurementServiceImpl implements BaseMeasurementService {

    @Autowired
    private BaseMeasurementMapper measurementMapper;

    @Override
    public void addMeasurement(BaseMeasurementAddDto measurementAddDto) {
        Integer uid = LoginDetailUtils.getCurrentEmployeeId();
        BaseMeasurement measurement = new BaseMeasurement();
        BeanUtils.copyProperties(measurementAddDto, measurement);
        if (uid != null) {
            measurement.setCreateBy(uid.longValue());
            measurement.setCreateTime(new Date());
            measurementMapper.addMeasurement(measurement);
        }
    }

    @Override
    public void deleteMeasurement(Long measurementId) {
        measurementMapper.deleteMeasurement(measurementId);
    }

    @Override
    public void updateMeasurement(BaseMeasurementAddDto measurementAddDto) {
        Integer uid = LoginDetailUtils.getCurrentEmployeeId();
        BaseMeasurement measurement = new BaseMeasurement();
        BeanUtils.copyProperties(measurementAddDto, measurement);
        if (uid != null) {
            measurement.setModifyBy(uid.longValue());
            measurement.setModifyTime(new Date());
            measurementMapper.updateMeasurement(measurement);
        }
    }

    @Override
    public Page<BaseMeasurement> queryMeasurement(Page<BaseMeasurement> page, BaseMeasurementQueryDto measurement) {
        List<BaseMeasurement> list = measurementMapper.queryMeasurement(page, measurement);
        return page.setRecords(list);
    }

    @Override
    public List<BaseMeasurementExport> queryMeasurementExport(BaseMeasurementQueryDto queryDto) {
        return measurementMapper.queryMeasurementExport(queryDto);
    }

    @Override
    public String getMeasurementCode() {
        return measurementMapper.getMeasurementCode();
    }
}
